package cn.com.dhc.danlu.widget.couponview.filterview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonConst.CouponConst;

/**
 * @author: 李一方
 * @time: 2016-09-22 15:31
 * @mail: liyifang@danlu.com
 * @desc: 筛选功能栏控件
 */
public class CouponFilterView extends LinearLayout {
    private View root;
    private TextView tvDefault;
    private LinearLayout llQuota;
    private LinearLayout llDeadline;
    private LinearLayout llFilter;

    private int lastClickViewID = R.id.tv_default;

    private OnFilterClickListener onFilterClickListener;

    public CouponFilterView(Context context) {
        this(context, null);
    }

    public CouponFilterView(Context context, AttributeSet attrs) {
        super(context, attrs);

        initViews();
        setViews();
    }

    private void initViews() {
        root = LayoutInflater.from(getContext()).inflate(R.layout.coupon_view_filter, this, true);
        tvDefault = (TextView) root.findViewById(R.id.tv_default);
        llQuota = (LinearLayout) root.findViewById(R.id.ll_quota);
        llDeadline = (LinearLayout) root.findViewById(R.id.ll_deadline);
        llFilter = (LinearLayout) root.findViewById(R.id.ll_filter);
        tvDefault.setHovered(true);
    }

    private void setViews() {
        tvDefault.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectState(v);
                onFilterClickListener.onDefaultCicked(v);
            }
        });

        llQuota.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectState(v);
                onFilterClickListener.onQuotaCicked(v, v.isSelected() ? CouponConst.DEALERS_SORT_DESC : CouponConst.DEALERS_SORT_ASC);
            }
        });

        llDeadline.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectState(v);
                onFilterClickListener.onDeadlineCicked(v, v.isSelected() ? CouponConst.DEALERS_SORT_DESC : CouponConst.DEALERS_SORT_ASC);
            }
        });

        llFilter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectState(v);
                v.setHovered(true);
                v.setSelected(true);
                onFilterClickListener.onFilterCicked(v);
            }
        });

    }

    public void setSelectState(View v) {
        if (this.lastClickViewID != v.getId()) {
            root.findViewById(this.lastClickViewID).setHovered(false);
            root.findViewById(this.lastClickViewID).setSelected(false);

            lastClickViewID = v.getId();
        }

        if (v == llFilter) {
            return;
        } else {
            v.setSelected(!v.isSelected());
            v.setHovered(true);
        }
    }

    /**
     * 获取默认高亮视图
     */
    public View getDefaultView() {
        return tvDefault;
    }

    public void canClickChildView(boolean canClick) {
        tvDefault.setEnabled(canClick);
        llQuota.setEnabled(canClick);
        llDeadline.setEnabled(canClick);
        llFilter.setEnabled(canClick);
    }

    /**
     * 设置过滤栏的类型
     *
     * @param type
     *         类型
     */
    public void setFilterType(FilterType type) {
        if (null != type) {
            switch (type) {
                case dealers_envelop: // 红包
                    llQuota.setVisibility(GONE);
                    llFilter.setVisibility(GONE);
                    break;

                case dealers_coupon: // 优惠券
                default:     // 默认状况
                    llQuota.setVisibility(VISIBLE);
                    llFilter.setVisibility(VISIBLE);
                    break;
            }
        }
    }

    public void setOnFilterClickListener(OnFilterClickListener onFilterClickListener) {
        this.onFilterClickListener = onFilterClickListener;
    }
}